from db.dao import PositionRecordDao, PositionDao
from db.models import StockPosition
from quantification.data_handler.DataHandler import DataHandler
from utils.util import CommonUtil, ObjDictTool


class Position(DataHandler):
    '''
    持仓仓位
    '''

    __slots__ = ('position_id', 'stock_code', 'stock_name', 'average_price', 'buy_time', 'hold_quantity',
                 'update_time')

    def __init__(self):
        self.calculate_average_priceposition_id = None

        self.positionRecords = None

        self.stock_code = None

        self.stock_name = None

        self.average_price = None

        # 第一次购买的时间
        self.buy_time = None

        # 持股数量
        self.hold_quantity = 0

        # 更新时间
        self.update_time = None

    def __init__(self, stock_code, stock_name):

        self.stock_code = stock_code

        self.stock_name = stock_name

        self.position_id = None

        self.positionRecords = []

        self.average_price = None

        # 第一次购买的时间
        self.buy_time = None

        # 持股数量
        self.hold_quantity = 0

        # 更新时间
        self.update_time = None

        self.load_data()

    def add_position_record(self, position_record):
        self.positionRecords.append(position_record)


    def load_data(self):
        positions = PositionDao().queryByParam(self.position_id, self.stock_code)
        if positions:
            position = positions[0]
            self.position_id = getattr(position, 'position_id')
            self.load_position_records()
            self.average_price = getattr(position, 'average_price')
            self.buy_time = getattr(position, 'buy_time')
            self.hold_quantity = getattr(position, 'hold_quantity')
            self.update_time = getattr(position, 'update_time')
        else:
            self.position_id = CommonUtil.get_uuid()

    def store_data(self):
        if self.hold_quantity == 0:
            PositionDao().removeByPositionId(self.position_id)
            return
        stock_position = StockPosition()
        ObjDictTool.copyA2B(self, stock_position)
        PositionDao().save(stock_position)

    def calculate_average_price(self):
        '''
        设置持仓平均价格
        :return: 
        '''
        if self.hold_quantity == 0:
            return
        if self.positionRecords:
            buySum = sum(float(getattr(positionRecord, 'price')) * getattr(positionRecord, 'amount') for positionRecord in
                         self.positionRecords if getattr(positionRecord, 'deal_direction') > 0)

            sellSum = sum(
                -1 * float(getattr(positionRecord, 'price')) * getattr(positionRecord, 'amount') for positionRecord in
                self.positionRecords if getattr(positionRecord, 'deal_direction') < 0)
            self.average_price = (buySum + sellSum) / self.hold_quantity

    def calculate_hold_quantity(self):
        buySum = sum(getattr(positionRecord, 'amount') for positionRecord in
                     self.positionRecords if getattr(positionRecord, 'deal_direction') > 0)

        sellSum = sum(-1 * getattr(positionRecord, 'amount') for positionRecord in
                      self.positionRecords if getattr(positionRecord, 'deal_direction') < 0)
        self.hold_quantity = buySum + sellSum

    def load_position_records(self):
        '''
        加载持仓历史表
        :return: 
        '''
        self.positionRecords = PositionRecordDao().queryByParam(self.position_id, None, None)
